/* 能够接受json数据 */

const http = require('http')

/*设置监听的端口*/
const port = 3000;

const server = http.createServer((req, res) => {
    const bufs = [];
    req.on('data', (buf) => {
        bufs.push(buf)
    });
    req.on('end', () => {
        const buf = Buffer.concat(bufs).toString('utf8')
        let msg = 'hello';
        try {
            const ret = JSON.parse(buf)
            msg = ret.msg;
        } catch (err) {
            //res.end('invalid json')
        }
        const responseJson = {
            msg: `receive your msg:${msg}`
        }
        res.setHeader('Content-Type', 'application/json')
        res.end(JSON.stringify(responseJson))
    })
})

server.listen(port, () => {
    console.log("listening on:", port)
})